/**
 * 
 */
package com.mobile2you.nci.android.data;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.mobile2you.nci.android.domain.Annuaires;
import com.mobile2you.nci.android.domain.Centres;
import com.mobile2you.nci.android.domain.News;
import com.mobile2you.nci.android.domain.Services;
import com.mobile2you.nci.android.utils.Utils;

/**
 * @author JBromo
 * 
 */
public class WebServicesRequest {

	public static void checkUpdate(Context context) {
		String url = "http://nci.m2y.fr/updated_at.json";
		String data = getHttpData(url, true);
		if (data != null) {
			try {
				JSONObject object = new JSONObject(data);
				DataParseur.parseUpdate(context, object);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}

	public static List<Centres> getCentres(Context context) {
		String url = "http://nci.m2y.fr/centres.json";
		try {
			String data = getHttpData(url);
			JSONArray jsonArray = new JSONArray();
			if (data != null) {
				jsonArray = new JSONArray(data);
			}
			if (jsonArray != null && jsonArray.length() > 1) {
				SqlHelper.getInstance(context).saveCentres(jsonArray);
			}
			return DataParseur.parseCentres(jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
			return new ArrayList<Centres>();
		}

	}

	public static String getHttpData(String url) {

		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httppost = new HttpGet(url);
			HttpResponse response = httpclient.execute(httppost);
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
			return new String();
		}

	}

	public static String getHttpData(String url, boolean withTimeOut) {

		try {
			HttpClient httpclient = new DefaultHttpClient();
			HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),
					10000);
			HttpGet httppost = new HttpGet(url);
			HttpResponse response = httpclient.execute(httppost);
			return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			return new String();
		}

	}

	public static List<Annuaires> getListAnnuaires(Context context,
			String login, String password) {
		String URL = "http://nci.m2y.fr/annuaire.json?u=" + login + "&p="
				+ Utils.getEncrytedPassword(password);
		List<Annuaires> list = new ArrayList<Annuaires>();
		try {
			JSONArray array = new JSONArray(getHttpData(URL));
			list = DataParseur.parseAnnuaire(array);
			if (list != null && !list.isEmpty()) {
				SqlHelper.getInstance(context).saveAnnuaire(array);
				SqlHelper.getInstance(context).saveLogin(login, password);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;

	}

	public static List<News> getNews(Context context) {
		String url = "http://nci.m2y.fr/news.json";
		try {
			String data = getHttpData(url);
			JSONArray jsonArray = new JSONArray();
			if (data != null) {
				jsonArray = new JSONArray(data);
			}
			if (jsonArray != null && jsonArray.length() > 1) {
				SqlHelper.getInstance(context).saveNews(jsonArray);
			}
			return DataParseur.parseNews(jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
			return new ArrayList<News>();
		}

	}

	public static List<Services> getServices(Context context) {
		String url = "http://nci.m2y.fr/services.json";
		try {
			String data = getHttpData(url);
			JSONArray jsonArray = new JSONArray();
			if (data != null) {
				jsonArray = new JSONArray(data);
			}
			if (jsonArray != null && jsonArray.length() > 1) {
				SqlHelper.getInstance(context).saveServices(jsonArray);
			}
			return DataParseur.parseServices(jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
			return new ArrayList<Services>();
		}

	}

	public static boolean sendContacts(JSONObject json) {
		HttpClient client = new DefaultHttpClient();
		String URL = "http://nci.m2y.fr/contacts.json";
		HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
																				// Limit
		HttpResponse response;

		try {
			HttpPost post = new HttpPost(URL);

			StringEntity se = new StringEntity(json.toString());
			se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json"));
			post.setEntity(se);
			response = client.execute(post);
			if (response != null) {
				String value = EntityUtils.toString(response.getEntity());
				return value != null && value.toLowerCase().contains("ok");

			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;

		}

	}

	public static boolean sendContacts(String nom, String telephone, String key) {
		HttpClient client = new DefaultHttpClient();
		String URL = "http://nci.m2y.fr/contacts.json";
		HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
																				// Limit
		HttpResponse response;
		JSONObject json = new JSONObject();
		try {
			HttpPost post = new HttpPost(URL);
			json.put("nom", nom);
			json.put("telephone", telephone);
			json.put("key", "fc68194ce2c81d3be065761c9292c4f5e9bade24");
			StringEntity se = new StringEntity(json.toString());
			se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json"));
			post.setEntity(se);
			response = client.execute(post);
			if (response != null) {
				String value = EntityUtils.toString(response.getEntity());
				System.out.println("Entity :" + value);
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;

		}

	}

}
